package pl.edu.agh.iosr.gamblingzone.userpanel.action;

import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherRequestDAO;
import pl.edu.agh.iosr.gamblingzone.dao.UserDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.MoneyVoucherRequestDAOBean;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.UserDAOBean;
import pl.edu.agh.iosr.gamblingzone.model.MoneyVoucherRequest;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;

@Stateless
@Name("voucherRequest")
public class MoneyVoucherRequestViewAction implements MoneyVoucherRequestView {
	
	@PersistenceContext
	private EntityManager em;
	
	@In(required=true)
	DAOFactory dao;
	
	@In
	@Out
	private User user;
	
	@Logger
	Log log;
	
	@In
	private FacesMessages facesMessages;
	
	private String value;
	
	public void submit() {
		if((value == null) || (value.length() == 0)) {
			facesMessages.add("Specify amount of money");
			return;
		}
		
		try {
			Long money = Long.valueOf(value);
			MoneyVoucherRequest voucherRequest = new MoneyVoucherRequest();
			
			MoneyVoucherRequestDAO daoRequest = dao.getVoucherRequestDAO();
			UserDAO userDAO = dao.getUserDAO();
			
			voucherRequest.setMoney(money);
			
			user = em.merge(user);
			user.addVoucherRequest(voucherRequest);
			
			facesMessages.add("You request was enqueued");
		} catch (NumberFormatException e) {
			facesMessages.add("Wrong format. Type integer value");
		}
	}
	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Remove
	public void destroy() {
	}
}
